package com.web.servlet;

import com.bean.User;
import com.dao.UserDao;
import com.dao.impl.GetPhoneNumber;
import com.web.common.exception.DataAccessException;
import com.web.common.utils.BeanFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author wangzhipeng
 * @Date 2020-08-31 1:43 下午
 * @Description
 */
@WebServlet(urlPatterns = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    private UserDao userDao = (UserDao) BeanFactory.getBean(BeanFactory.USER_DAO);
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
       // String name = new String(request.getParameter("name").getBytes("iso8859-1"),"UTF-8");
      String name = request.getParameter("name");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String phone =request.getParameter("phone");
        User user = new User(-1,name,password,phone,email);
        int i=0;
        try {
            i = userDao.registerUser(user);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter printWriter = response.getWriter();
        if(i==1){
            printWriter.println("注册成功");
        }else {
            printWriter.println("注册失败！");
        }
        printWriter.flush();
        printWriter.close();
    }
}
